package com.wuuxiang.polestar.admin.web;

import com.wuuxiang.polestar.admin.dto.BaseReturnDTO;
import com.wuuxiang.polestar.admin.dto.SecurityDto;
import com.wuuxiang.polestar.admin.dto.SecurityRetDto;
import com.wuuxiang.polestar.admin.service.security.ISecurityService;
import com.wuuxiang.polestar.admin.utils.OvalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 项目名称：polestar        <br>
 * 类描述：                         <br>
 * 创建人：夏鸿鹏                    <br>
 * 创建时间：2017-11-28 14:48        <br>
 * 修改备注：                        <br>
 */
@RestController
public class SecurityController {

    @Autowired
    private ISecurityService securityService;

    @RequestMapping("/polestar/createSecurity")
    @ResponseBody
    public BaseReturnDTO createProperty(@RequestBody SecurityDto securityDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, securityDto)) {
            return baseReturnDTO;
        }
        String key = securityDto.getKey();
        if (securityService.checkExists(key)) {
            baseReturnDTO.setReturnCode(-1);
            baseReturnDTO.setErrorCode("500");
            baseReturnDTO.setErrorText("key已经存在!");
        } else {
            securityService.createSecurity(key);
        }
        return baseReturnDTO;
    }

    @RequestMapping("/polestar/removeSecurity")
    @ResponseBody
    public BaseReturnDTO removeSecurity(@RequestBody SecurityDto securityDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, securityDto)) {
            return baseReturnDTO;
        }
        String key = securityDto.getKey();
        if (securityService.checkExists(key)) {
            securityService.removeSecurity(key);
        }
        return baseReturnDTO;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/polestar/getSecurityLst")
    @ResponseBody
    public SecurityRetDto getSecurityLst() {
        SecurityRetDto securityRetDto = new SecurityRetDto();
        securityRetDto.setSecurityLst(securityService.getSecurityLst());
        return securityRetDto;
    }

}
